<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="24">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag :disabled="formDisabled" type="radio" v-model="model.type" dictCode="festival_type" placeholder="请选择类型"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="节假日名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input :disabled=formDisabled v-model="model.name" placeholder="请输入节假日名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginTime">
              <a-date-picker :disabled=formDisabled placeholder="请选择开始时间" @change="handleChange" v-model="model.beginTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <a-date-picker :disabled=formDisabled
                  :disabled-date="disabledDate"
                  :default-picker-value="this.model.beginTime"
                  placeholder="请选择结束时间" v-model="model.endTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-textarea :disabled=formDisabled v-model="model.description" placeholder="请输入描述"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import moment from 'moment'
  import { validateDuplicateValue, validateBlankValue } from '@/utils/util'

  export default {
    name: 'ErpFestivalForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        dateFlag: false,
        dateList: [],
        fromDateList: [],
        model:{
          type: 1
        },
        defaultValue:'',
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
          name: [
            { required: true, message: '请输入节假日名称!' },
            { validator: (rule, value, callback) => validateBlankValue('erp_festival', 'name', value, this.model.id, callback) },
          ],
          beginTime: [
            { required: true, message: '请选择开始时间!' }
          ],
          endTime: [
            { required: true, message: '请选择结束时间!' }
          ],
          type: [
            { required: true, message: '请选择类型!' }
          ],
        },
        url: {
          add: "/api/erp/hr/erpFestival/add",
          edit: "/api/erp/hr/erpFestival/edit",
          queryById: "/api/erp/hr/erpFestival/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      moment,
      handleChange(value) {
        if (this.model.endTime) {
          if (moment(this.model.endTime).diff(moment(this.model.beginTime), 'days') < 0 ) {
            this.model.endTime = this.model.beginTime
          }
        }
      },
      disabledDate(current) {
        return current < moment(this.model.beginTime).subtract('day')
      },
      add (item) {
        this.modelDefault.beginTime = item
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        if (record.beginTime && record.endTime) {
          this.getAllDate(record, 1)
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let item = Object.assign({}, this.model);
            this.getAllDate(item, 0)
            if(this.dateFlag) {
              let httpurl = '';
              let method = '';
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
              }else{
                httpurl+=this.url.edit;
                method = 'put';
              }
              this.model.beginTime = moment(this.model.beginTime).format('YYYY-MM-DD')
              this.model.endTime = moment(this.model.endTime).format('YYYY-MM-DD')
              httpAction(httpurl,this.model,method).then((res)=>{
                if(res.success){
                  that.$message.success(res.message);
                  that.$emit('ok');
                }else{
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.confirmLoading = false;
              })
            } else {
              that.confirmLoading = false;
              that.$message.warning('请检查时间是否交叉！');
            }
          }
        })
      },
      //封装
      getAllDate (item, num) {
        this.fromDateList = []
        item.beginTime = moment(item.beginTime).format('YYYY-MM-DD')
        item.endTime = moment(item.endTime).format('YYYY-MM-DD')
        var getDate = function(str) {
          var tempDate = new Date();
          var list = str.split("-");
          tempDate.setFullYear(list[0]);
          tempDate.setMonth(list[1] - 1);
          tempDate.setDate(list[2]);
          return tempDate;
        }
        var date1 = getDate(item.beginTime);
        var date2 = getDate(item.endTime);
        if (date1 > date2) {
          var tempDate = date1;
          date1 = date2;
          date2 = tempDate;
        }
        date1.setDate(date1.getDate());
        var i = 0;
        while (!(date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate())) {
          var dayStr =date1.getDate().toString();
          if(dayStr.length ==1){
            dayStr="0"+dayStr;
          }
          var m = date1.getMonth() + 1
          this.fromDateList.push(date1.getFullYear() + "-" + (m < 10 ? '0' + m : m) + "-" + dayStr)
          date1.setDate(date1.getDate() + 1);
          i++;
        }
        this.fromDateList.push(item.endTime)

        if(num > 0) {
          this.dateList = this.dateList.filter(v =>
            this.fromDateList.every(n => {
              return n !== v
            })
          )
        } else {
          let dates = this.dateList.filter(v =>
            this.fromDateList.some(n => {
              return n == v
            })
          )
          if(dates.length > 0) {
            this.dateFlag = false
          } else {
            this.dateFlag = true
          }
        }
      }
    }
  }
</script>
<style >

</style>